package com.example.gybicycle.controller.wx;

import com.example.gybicycle.common.entity.Entity;
import com.example.gybicycle.common.entity.NormalPage;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.util.BiCycleUtil;
import com.example.gybicycle.common.util.CyclingUtil;
import com.example.gybicycle.common.util.LoginUtil;
import com.example.gybicycle.common.wxPay.WechatPayExternalService;
import com.example.gybicycle.entity.*;
import com.example.gybicycle.entity.wx.WXLoginInfo;
import com.example.gybicycle.service.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

@Tag(name = "我的")
@RestController
@RequestMapping("/wx/my")
public class MyController {
    @Autowired
    private MemberOrderService orderService;
    @Autowired
    private MemberInsureService insureService;
    @Autowired
    private MemberDepositService depositService;
    @Autowired
    private MemberPayService payService;
    @Autowired
    private MemberCyclingService cyclingService;
    @Autowired
    private WechatPayExternalService wxpayService;
    @PostMapping("/info")
    @Operation(summary = "个人信息")
    public Result<WXLoginInfo> info(HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        return Result.success(info);
    }
    @PostMapping("/order")
    @Operation(summary = "订单")
    public Result<NormalPage<MemberOrder>> page(@RequestBody Entity entity, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        MemberOrder page=new MemberOrder();
        BeanUtils.copyProperties(entity,page);
        page.setMemberId(info.getId());
        switch (entity.getType()){
            case 2:
//                待支付
                page.setOrderState(1);
                page.setPayState(0);
                break;
            case 3:
//                进行中
                page.setOrderState(0);
                break;
            case 4:
//                已完成
                page.setOrderState(1);
                page.setPayState(1);
                break;
        }
        Page<MemberOrder> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
                () ->orderService.findList(page));
        for(MemberOrder order:list){
            order.setStateName(CyclingUtil.setOrderState(order));
            order.setOrderState(CyclingUtil.setOrderStateByMy(order));
            order.setInsureMoney(new BigDecimal(1));
            order.setCyclingMoney(order.getPayMoney().subtract(order.getInsureMoney()));

        }
        return Result.success(NormalPage.createPage(list));
    }

    @PostMapping("/payorderNum")
    @Operation(summary = "待支付订单数")
    public Result payorderNum(@RequestBody Entity entity, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        MemberOrder page=new MemberOrder();
        BeanUtils.copyProperties(entity,page);
        page.setMemberId(info.getId());
        page.setOrderState(1);
        page.setPayState(0);
       int num= orderService.count(page);
        return Result.success(num);
    }
    @PostMapping("/cycling")
    @Operation(summary = "骑行记录")
    public Result<NormalPage<MemberCycling>> cycling(@RequestBody Entity entity, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        MemberCycling page=new MemberCycling();
        BeanUtils.copyProperties(entity,page);
        page.setMemberId(info.getId());
        Page<MemberCycling> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
                () ->cyclingService.findList(page));
        return Result.success(NormalPage.createPage(list));
    }
    @PostMapping("/insure")
    @Operation(summary = "保单管理")
    public Result<NormalPage<MemberInsure>> insure(@RequestBody Entity entity, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        MemberInsure page=new MemberInsure();
        BeanUtils.copyProperties(entity,page);
        page.setMemberId(info.getId());
        Page<MemberInsure> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
                () ->insureService.findList(page));
        return Result.success(NormalPage.createPage(list));
    }
    @PostMapping("/deposit")
    @Operation(summary = "押金管理")
    public Result<NormalPage<MemberDeposit>> Deposit(@RequestBody Entity entity, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        MemberDeposit page=new MemberDeposit();
        BeanUtils.copyProperties(entity,page);
        page.setMemberId(info.getId());
        Page<MemberDeposit> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
                () ->depositService.findList(page));
        return Result.success(NormalPage.createPage(list));
    }
    @PostMapping("/depositMoney")
    @Operation(summary = "押金金额")
    public Result depositMoney(HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        MemberDepositMoney money=depositService.depositMoney(info.getId());
        return Result.success(money);
    }
    @PostMapping("/pay")
    @Operation(summary = "支付管理")
    public Result<NormalPage<MemberPay>> pay(@RequestBody Entity entity, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        MemberPay page=new MemberPay();
        BeanUtils.copyProperties(entity,page);
        page.setMemberId(info.getId());
//        page.setPayState(1);
        Page<MemberPay> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
                () ->payService.findList(page));
        return Result.success(NormalPage.createPage(list));
    }
    @PostMapping("/payMoney")
    @Operation(summary = "支付金额")
    public Result pay(@RequestBody MemberPay pay, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        pay=payService.get(pay.getId());
        pay.setMemberOpenId(info.getWxOpenid());
        var re=wxpayService.prepayWithRequestPayment(pay);
        return Result.success(re);
    }

}
